package sdu.se;



import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;

import android.graphics.Color;

import android.os.Bundle;

import android.util.Log;
import android.util.LogPrinter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class StartTestActivity extends Activity {
	public Properties p;
	SingleChoiseQuestion q;
	Test t;
	RadioButton[]rb;
	TextView Qnofm;
	TextView QuestionText;
	TextView AnswerText;
	Button next;
	Button prev;
	TextView NameTextTest;
	TextView TimeTextTest;
	void init(){
		
		rb=new RadioButton[26];
        rb[0]=(RadioButton)findViewById(R.id.radioButton1);
        rb[1]=(RadioButton)findViewById(R.id.radioButton2);
        rb[2]=(RadioButton)findViewById(R.id.radioButton3);
        rb[3]=(RadioButton)findViewById(R.id.radioButton4);
        rb[4]=(RadioButton)findViewById(R.id.radioButton5);
        rb[5]=(RadioButton)findViewById(R.id.radioButton6);
        rb[6]=(RadioButton)findViewById(R.id.radioButton7);
        rb[7]=(RadioButton)findViewById(R.id.radioButton8);
        rb[8]=(RadioButton)findViewById(R.id.radioButton9);
        rb[9]=(RadioButton)findViewById(R.id.radioButton10);
        rb[10]=(RadioButton)findViewById(R.id.radioButton11);
        rb[11]=(RadioButton)findViewById(R.id.radioButton12);
        rb[12]=(RadioButton)findViewById(R.id.radioButton13);
        rb[13]=(RadioButton)findViewById(R.id.radioButton14);
        rb[14]=(RadioButton)findViewById(R.id.radioButton15);
        rb[15]=(RadioButton)findViewById(R.id.radioButton16);
        rb[16]=(RadioButton)findViewById(R.id.radioButton17);
        rb[17]=(RadioButton)findViewById(R.id.radioButton18);
        rb[18]=(RadioButton)findViewById(R.id.radioButton19);
        rb[19]=(RadioButton)findViewById(R.id.radioButton20);
        rb[20]=(RadioButton)findViewById(R.id.radioButton21);
        rb[21]=(RadioButton)findViewById(R.id.radioButton22);
        rb[22]=(RadioButton)findViewById(R.id.radioButton23);
        rb[23]=(RadioButton)findViewById(R.id.radioButton24);
        rb[24]=(RadioButton)findViewById(R.id.radioButton25);
        rb[25]=(RadioButton)findViewById(R.id.radioButton26);
        Qnofm=(TextView)findViewById(R.id.Qnofm);
        QuestionText=(TextView)findViewById(R.id.QuestionText);
        AnswerText=(TextView)findViewById(R.id.AnswerText);
        NameTextTest=(TextView)findViewById(R.id.NameTextTest);
        next = (Button) findViewById(R.id.NextQ);
        prev=(Button)findViewById(R.id.PrevQ);
        TimeTextTest=(TextView)findViewById(R.id.TimeTextTest);
	
        next.setText("Next");
        prev.setText("Previous");
	}
	void load(Test tt){
		resetRadio();
		Qnofm.setText(""+(tt.getPosition()+1)+" of "+tt.getAll().length);
		SingleChoiseQuestion qq=(SingleChoiseQuestion)tt.getCurrent();
        QuestionText.setText(qq.questionBodyText);
        QuestionText.setVisibility(1);
        String ans="";
        for(int i=0;i<26;i++){
        	if((qq.correct&(1<<i))!=0){
        		ans+=""+(char)(((int)'A')+i)+" ";
        		if(qq.anwered)rb[i].setTextColor(Color.GREEN);
        	}
        
        }
        ans+="\n"+qq.answerBodyText;
        AnswerText.setText(ans);
        if(qq.anwered)AnswerText.setVisibility(1);
        else AnswerText.setVisibility(8);
        setRadio(qq);
		
	}
	void resetRadio(){
		
		for(int i=0;i<26;i++){
        	rb[i].setVisibility(8);
        	rb[i].setChecked(false);
        	rb[i].setTextColor(Color.WHITE);
        	
        }
		
	}
	void setRadio(SingleChoiseQuestion quest){
		
		for(int i=0;i<quest.choises.length;i++){
        	rb[i].setText(quest.choises[i]);
        	rb[i].setVisibility(1);
        	if((quest.choised&(1<<i))!=0)rb[i].setChecked(true);
            
        	
        }
		
	}
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question);
        //RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
        init();
        
        NameTextTest.setText("Sample Test");
        
        TimeTextTest.setText("infinite");
        
        t=new Sample().getSampleTest();
        load(t);
        
        
        
        
        
        
        
        
        next.setOnClickListener(new View.OnClickListener() {
            
                	public void onClick(View v) {
                    	int z=0;
                    	for(int i=0;i<26;i++){
                        	if(rb[i].isChecked())z+=(1<<i);
                    	}
                    	if(!t.getCurrent().anwered)t.setMarkCurrent(z);
                    	t.next();
                		load(t);
                    	
                    }
        });
        
        prev.setOnClickListener(new View.OnClickListener() {
            
		        	public void onClick(View v) {
		            	int z=0;
		            	for(int i=0;i<26;i++){
		                	if(rb[i].isChecked())z+=(1<<i);
		            	}
		            	if(!t.getCurrent().anwered)t.setMarkCurrent(z);
		            	t.prev();
		        		load(t);
		            	
		            }
        });
        //rg.addView(rb);
         
    }
	
	boolean check(){
		boolean s=true;
		Question[] qq=t.getAll();
		for(int i=0;i<qq.length;i++){
			s=s&qq[i].anwered;
		}
		return s;
	}

	void setAnswered(){
		int z=0;
    	for(int i=0;i<26;i++){
        	if(rb[i].isChecked())z+=(int)Math.pow(2, i);
    	}
    	Log.d("setAns", ""+z);
    	if(!t.getCurrent().anwered)t.setMarkCurrent(z);
		t.setCurrentAnswered();
		SingleChoiseQuestion qq=(SingleChoiseQuestion)t.getCurrent();
		String ans="";
		for(int i=0;i<26;i++){
        	if((qq.correct&(1<<i))!=0){
        		ans+=""+(char)(((int)'A')+i)+" ";
        		if(qq.anwered)rb[i].setTextColor(Color.GREEN);
        	}
        	else if(rb[i].isChecked())rb[i].setTextColor(Color.RED);
        
        }
		
        ans+="\n"+qq.answerBodyText;
        AnswerText.setText(ans);
        if(qq.anwered)AnswerText.setVisibility(1);
		if(check()){
			
			showDialog(0);
			
		}
		
	}
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.qmenu, menu);
        return true;
    }
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.item1:
        	setAnswered();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
	
	@Override
	protected Dialog onCreateDialog(int id)
	{
	    DialogInterface.OnClickListener doNothing = new DialogInterface.OnClickListener()
	    {
	        public void onClick(DialogInterface dialog, int whichButton)
	        {
	        }
	    };
	    String alertMessage;
	 
	    switch (id)
	    {
	        case 0:
	            Question[] qq=t.getAll();
	            int i=0;
	            for(int j=0;j<qq.length;j++)
	            	if(((SingleChoiseQuestion)qq[j]).correct==((SingleChoiseQuestion)qq[j]).choised)i++;
	        	alertMessage = "Congratulations!\n"+(""+i)+" of "+(""+qq.length)+" is correct";
	            break;
	        
	        default:
	            return null;
	    }
	    Builder b=new Builder(this);
//	    builder.setTitle("Input Alert")
//		.setMessage("Please enter something")
//		.setPositiveButton("Ok", onClickListener)
//		.setView(editText).create();
//	    return new AlertDialog(this)
//	        .setMessage(alertMessage)
//	        .setNeutralButton(R.string.ok, doNothing)
//	        .create();
	    return b.setTitle("Done").setMessage(alertMessage).setNeutralButton("ok", doNothing).create();
	}
}
